import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

// 公共路由
export const constantRoutes = [
    {
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [
            {
                path: '/redirect/:path(.*)',
                component: () => import('@/pages/redirect/index.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/pages/login/index.vue'),
        hidden: true,
        meta: {
            title: '登录',
        }
    },
    {
        path: "/:pathMatch(.*)*",
        component: () => import('@/pages/error/404.vue'),
        hidden: true
    },
    {
        path: '',
        component: Layout,
        redirect: '/index',
        hidden: false,
        meta: { title: '主页', icon: 'icon-menu-home', affix: true },
        children: [
            {
                path: '/index',
                component: () => import('@/pages/home/index.vue'),
                name: 'Index',
                meta: { title: '主页', icon: 'icon-menu-home', affix: true }
            }
        ]
    },

]

const router = createRouter({
    history: createWebHistory(),
    routes: constantRoutes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { top: 0 }
        }
    },
})

export default router